package graph;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;

/**
 * @ author : HyoJoo-W
 * @ createDate : 2021-06-06 13:54
 * @ Description :
 */
public class Graph
{
    private ArrayList<String> vertexList;
    private int[][] edges;
    private int numOfEdges;//表示边的数目

    private boolean[] isVisited;//记录某个节点是否被访问

    public static void main(String[] args)
    {
        int vertexNum = 8;
        //String[] vertexValue = {"A", "B", "C", "D", "E"};
        String[] vertexValue = {"1", "2", "3", "4", "5", "6", "7", "8"};
        Graph graph = new Graph(vertexNum);
        for (String s : vertexValue)
        {
            graph.insertVertex(s);
        }
        //graph.insertEdge(0, 1, 1);
        //graph.insertEdge(0, 2, 1);
        //graph.insertEdge(1, 2, 1);
        //graph.insertEdge(1, 3, 1);
        //graph.insertEdge(1, 4, 1);
        graph.insertEdge(0,1,1);
        graph.insertEdge(0,2,1);
        graph.insertEdge(1,3,1);
        graph.insertEdge(1,4,1);
        graph.insertEdge(3,7,1);
        graph.insertEdge(4,7,1);
        graph.insertEdge(2,5,1);
        graph.insertEdge(2,6,1);
        graph.insertEdge(5,6,1);

        graph.display();

        System.out.println("深度优先遍历");
        graph.dfs();

        System.out.println("广度优先遍历");
        graph.bfs();

    }

    //n: 顶点个数
    public Graph(int n)
    {
        edges = new int[n][n];
        vertexList = new ArrayList<>(n);
        numOfEdges = 0;
        isVisited = new boolean[n];
    }

    //插入顶点
    public void insertVertex(String vertex)
    {
        vertexList.add(vertex);
    }

    //插入边
    public void insertEdge(int v1, int v2, int weight)
    {
        edges[v1][v2] = weight;
        edges[v2][v1] = weight;
        numOfEdges++;
    }


    public int getNumOfVertex()
    {
        return vertexList.size();
    }

    public int getNumOfEdges()
    {
        return numOfEdges;
    }

    public String getValueByIndex(int index)
    {
        return vertexList.get(index);
    }

    //获取边的权值
    public int getWeight(int v1, int v2)
    {
        return edges[v1][v2];
    }

    public void display()
    {
        for (int[] row : edges)
        {
            System.out.println(Arrays.toString(row));
        }
    }

    //返回当前索引index对应节点的第一个邻接节点的索引
    public int getFirstNeighborIndex(int index)
    {
        for (int i = 0; i < vertexList.size(); i++)
        {
            if (edges[index][i] > 0)
            {
                return i;
            }
        }
        return -1;
    }

    //根据前一个邻接节点的二维下标获取下一个邻接节点的索引
    public int getNextNeighbor(int v1, int v2)
    {
        for (int i = v2 + 1; i < vertexList.size(); i++)
        {
            if (edges[v1][i] > 0)
            {
                return i;
            }
        }
        return -1;
    }

    //单个节点处理
    public void dfs(boolean[] isVisited, int i)
    {
        System.out.print(getValueByIndex(i) + "->");
        isVisited[i] = true;

        //获取节点i的第一个邻接节点
        int w = getFirstNeighborIndex(i);

        while (w != -1)
        {
            if (!isVisited[w])
            {
                dfs(isVisited, w);
            }
            //如果w被访问过,寻找(i,w)的下一个邻接节点
            w = getNextNeighbor(i, w);
        }
    }

    //遍历所有节点
    public void dfs()
    {
        isVisited = new boolean[vertexList.size()];
        for (int i = 0; i < getNumOfVertex(); i++)
        {
            if (!isVisited[i])
            {
                dfs(isVisited, i);
            }
        }
    }

    public void bfs(boolean[] isVisited, int i)
    {
        int u;//队列头节点索引
        int w;//邻接节点索引
        LinkedList<Integer> queue = new LinkedList<>();//记录节点访问的顺序

        System.out.print(getValueByIndex(i) + "->");
        isVisited[i] = true;


        queue.addLast(i);

        while (!queue.isEmpty())
        {
            u = queue.removeFirst();//取出队列头结点索引
            w = getFirstNeighborIndex(u);

            while (w != -1)
            {
                if (!isVisited[w])
                {
                    System.out.print(getValueByIndex(w) + "->");
                    isVisited[w] = true;
                    queue.addLast(w);
                }
                //已经访问过
                w = getNextNeighbor(u, w);

            }
        }
    }

    public void bfs()
    {
        isVisited = new boolean[vertexList.size()];
        for (int i = 0; i < getNumOfVertex(); i++)
        {
            if (!isVisited[i])
            {
                bfs(isVisited, i);
            }
        }
    }
}
